<template>
  <page-view :title="false">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="券名">
                <a-input v-model="queryParam.name" placeholder />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.enable" placeholder="请选择" default-value="0">
                  <a-select-option value="true">启用</a-select-option>
                  <a-select-option value="false">禁用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="{ float: 'right', overflow: 'hidden' }"
              >
                <a-button type="primary" @click="loadTableData()">查询</a-button>
                <a-button
                  style="margin-left: 8px"
                  @click="() => queryParam = {pageNo: 1, pageSize: 10}"
                >重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" @click="handleCreate()">新增</a-button>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :rowKey="record => record.id"
        :dataSource="couponList"
        :pagination="pagination"
        :scroll="{ x: 1800 }"
        @change="handleTableChange"
      >
        <span slot="enable" slot-scope="text">{{ text | enableFilter }}</span>

        <span slot="action" slot-scope="text, record">
          <a
            style="margin-right:10px;"
            href="javascript:;"
            v-action:promotioncouponEdit
            @click="handleEdit(record)"
          >编辑</a>
          <a href="javascript:;" @click="showSendModal(record)">发券</a>
        </span>
        <span slot="image" slot-scope="text">
          <img key="couponImage" :src="text" />
        </span>
      </a-table>

      <coupon-modal ref="couponModal" @ok="handleSaveOk" />

      <send-coupon-modal ref="sendCouponModal" @ok="handleSendCouponOk" />
    </a-card>
  </page-view>
</template>
<script>
import { PageView } from '@/layouts'
import CouponModal from './CouponModal'
import SendCouponModal from './SendCouponModal'
import { couponPage, couponDelete } from '@/api/promotion'

export default {
  name: 'coupon',
  components: {
    PageView,
    CouponModal,
    SendCouponModal
  },
  data() {
    return {
      columns,
      pagination: {},
      couponList: [],
      queryParam: { pageNo: 1, pageSize: 10 }
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination)
      const pager = { ...this.pagination }
      pager.current = pagination.current
      this.pagination = pager
      this.queryParam = {
        pageSize: pagination.pageSize,
        pageNo: pagination.current,
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters
      }
      this.loadTableData()
    },
    handleEdit(record) {
      this.$refs.couponModal.handleEdit(record)
    },
    handleCreate() {
      this.$refs.couponModal.handleCreate()
    },
    handleDel(record) {
      couponDelete(record).then(res => {
        if (res.status === 0) {
          this.loadTableData()
          this.$message.success('成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // handler
    handleSaveOk() {
      console.log('接受子組件的通知')
      this.loadTableData()
    },
    // handler
    handleSendCouponOk() {
      console.log('接受子組件的通知')
      this.loadTableData()
    },
    loadTableData() {
      var _this = this
      couponPage(this.queryParam).then(res => {
        const result = res.data
        _this.couponList = result.records
        _this.pagination = {
          total: result.total,
          pageSize: result.size,
          current: result.current
        }
        console.log(_this.pagination)
      })
    },
    showSendModal(record) {
      this.$refs.sendCouponModal.sendCoupon(record)
    }
  },
  created() {
    this.loadTableData()
  },
  filters: {
    enableFilter(enable) {
      const enableMap = {
        true: '启用',
        false: '禁用'
      }
      return enableMap[enable]
    }
  }
}

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    key: 'id',
    width: '12%'
  },
  {
    title: '优惠券名称',
    dataIndex: 'name',
    key: 'name',
    width: '12%'
  },
  {
    title: '优惠券类型',
    dataIndex: 'couponType',
    key: 'couponType'
  },

  {
    title: '库存数量',
    dataIndex: 'stock',
    key: 'stock'
  },
  {
    title: '优惠券面值',
    dataIndex: 'couponValue',
    key: 'couponValue'
  },
  {
    title: '创建者',
    dataIndex: 'creater',
    key: 'creater',
    width: '10%'
  },
  {
    title: '更新者',
    dataIndex: 'updater',
    key: 'updater',
    width: '10%'
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'action' },
    width: '20%'
  }
]
</script>
